//
// Created by wwwsp on 5/2/2022.
//

#include "Char_seq_queue.h"

Char_seq_queue::Char_seq_queue() {
    queue = new char[MAX_SIZE];
    head = 0;
    tail = 1;
    length = 0;
}

bool Char_seq_queue::isEmpty() {
    return (length == 0);
}

bool Char_seq_queue::isFull() {
    return (length == MAX_SIZE);
}

void Char_seq_queue::post(char data) {
    if(isFull()) return;
    if(isEmpty()){
        queue[head] = data;
        ++length;
        return;
    }
    queue[tail] = data;
    tail = (tail + 1) % MAX_SIZE;
    ++length;
}

char Char_seq_queue::pool() {
    if(isEmpty()) return '\0';
    char rs = queue[head];
    if(length == 1){
        queue[head] = '\0';
        --length;
        return rs;
    }
    head = (head + 1) % MAX_SIZE;
    --length;
    return rs;
}

Char_seq_queue::~Char_seq_queue() {
    delete [] queue;
}

void Char_seq_queue::print() {
    if(isEmpty()) return;
    for(int i = head; i != tail; i = (i + 1) % MAX_SIZE){
        std::cout << queue[i] << " ";
    }
}
